package com.grad.webstudy.controller;

import com.grad.webstudy.annota.ApiAuthorization;
import com.grad.webstudy.constants.AuthConstants;
import com.grad.webstudy.constants.FileContants;
import com.grad.webstudy.model.CourseDetail;
import com.grad.webstudy.service.CourseDetailService;
import com.grad.webstudy.utils.FileUtils;
import com.grad.webstudy.utils.VideoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.undo.AbstractUndoableEdit;
import java.io.IOException;
import java.util.List;

/**
 * @Description:
 * @Author: zjf
 * @Date: 2020/2/9 10:55.
 */

@RestController
@RequestMapping("/courseDetail")
public class CourseDetailController {


    @Autowired
    private CourseDetailService courseDetailService;



    @PostMapping("/add")
    @ApiAuthorization(auth = AuthConstants.COURSE_ADD)
    public int addCourseDetail(CourseDetail courseDetail) {
        return courseDetailService.add(courseDetail);
    }


    @PostMapping("/addList")
    @ApiAuthorization(auth = AuthConstants.COURSE_ADD)
    public boolean addCourseDetailList(List<CourseDetail> courseDetails, MultipartFile[] videos){
        List<String> paths = FileUtils.upload(videos, FileContants.COURSE_VIDEO_PATH);

        for (int i = 0; i < courseDetails.size(); i++) {
            courseDetails.get(i).setVideoPath(paths.get(i));
            courseDetails.get(i).setVideoLength(VideoUtils.getVideoTime(paths.get(i)));
        }
        return courseDetailService.addDetails(courseDetails);
    }


    @DeleteMapping("/deleteByIds")
    @ApiAuthorization(auth = AuthConstants.COURSE_DELETE)
    public boolean deleteByIds(String ids) {
        return courseDetailService.deletes(ids);
    }


    @DeleteMapping("/deleteByChapterId")
    @ApiAuthorization(auth = AuthConstants.COURSE_DELETE)
    public boolean deleteByChapterId(Integer chapterId) {
        return courseDetailService.deleteByChapter(chapterId);
    }

    @PutMapping("/update")
    @ApiAuthorization(auth = AuthConstants.COURSE_UPDATE)
    public boolean update(CourseDetail courseDetail) {
        return courseDetailService.update(courseDetail);
    }


    @PostMapping("/upload")
    @ApiAuthorization(auth = AuthConstants.COURSE_UPDATE)
    public String update(Integer detailId, MultipartFile video) {
        CourseDetail courseDetail=new CourseDetail();
        courseDetail.setId(detailId);
        List<String> paths = FileUtils.upload(new MultipartFile[]{video}, FileContants.COURSE_VIDEO_PATH);
        String path=paths.get(0);
        courseDetail.setVideoPath(path);
        courseDetail.setVideoLength(VideoUtils.getVideoTime(FileUtils.destFilePath));
        courseDetailService.update(courseDetail);
        return courseDetail.getVideoPath();
    }

    @GetMapping("/queryByChapterId")
    @ApiAuthorization(auth = AuthConstants.COURSE_MANAGER)
    public List<CourseDetail> queryByChapterId(Integer chapterId) {
        return courseDetailService.queryByChapterId(chapterId);
    }

    @GetMapping("/queryById")
    @ApiAuthorization(auth = AuthConstants.COURSE_MANAGER)
    public CourseDetail queryById(Integer courseDetailId) {
        return courseDetailService.queryById(courseDetailId);
    }


    @GetMapping("/queryByIdForUser")
    public CourseDetail queryByIdForUser(Integer courseDetailId) {

        return courseDetailService.queryById(courseDetailId);
    }
}
